package com.lich.controller;

import java.util.Date;
import java.util.List;
import java.util.UUID;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.json.MappingJackson2JsonView;

import com.lich.entity.Product;
import com.github.pagehelper.PageInfo;
import com.lich.entity.Department;
import com.lich.entity.vo.ProductResult;
import com.lich.entity.vo.Result;
import com.lich.service.DepartmentService;
import com.lich.service.ProductService;
import com.lich.util.JsonUtils;

@Controller
@RequestMapping("/product")
public class ProductController {

	private static Logger logger = LoggerFactory.getLogger(ProductController.class);
	@Resource
	ProductService productService;
	@Resource
	DepartmentService departmentService;

	/**
	 * @description 部门列表
	 * @date 2016年9月1日
	 * @author lich-thinkpad
	 * @return
	 */
	@RequestMapping(value = "/department", method = RequestMethod.GET)
	public ModelAndView departmentPage() {
		Result<List<Department>> result = new Result<>();
		List<Department> departments = departmentService.getAll();
		result.setObject(departments);
		result.setSuccess(true);
		result.setMessage("查询部门列表成功");
		return new ModelAndView("manager/product/department", "result", result);
	}

	/**
	 * @description 查找所有的部门
	 * @date 2016年9月6日
	 * @author lich-thinkpad
	 * @return
	 */
	@RequestMapping(value = "/department/all", method = RequestMethod.GET)
	public ModelAndView getAllDepartment() {
		Result<List<Department>> result = new Result<>();
		List<Department> departments = departmentService.getAll();
		result.setObject(departments);
		result.setSuccess(true);
		result.setMessage("查询部门列表成功");
		return new ModelAndView(new MappingJackson2JsonView(), "result", result);
	}

	/**
	 * @description 添加部门
	 * @date 2016年9月1日
	 * @author lich-thinkpad
	 * @param department
	 * @return
	 */
	// FIXME 添加部门没有检查部门名称是否重复
	@RequestMapping(value = "/department/add", method = RequestMethod.POST)
	public ModelAndView addDepartment(Department department) {
		Result<Department> result = new Result<>();

		Department oldDepartment = departmentService.getByName(department.getName());
		if (oldDepartment != null) {
			result.setSuccess(false);
			result.setMessage("部门名已存在");
			result.setObject(oldDepartment);
			logger.warn("部门名已存在，入参" + JsonUtils.object2Json(department));
			return new ModelAndView(new MappingJackson2JsonView(), "result", result);
		}
		// 暂时只有一级目录
		department.setLevel(1);
		department.setParentid(0);
		if (departmentService.add(department) > 0) {
			result.setSuccess(true);
			result.setMessage("添加部门信息成功");
		} else {
			result.setSuccess(false);
			result.setMessage("添加部门信息失败");
			logger.error("添加部门信息失败，入参" + JsonUtils.object2Json(department));
		}
		return new ModelAndView(new MappingJackson2JsonView(), "result", result);
	}

	/**
	 * @description 更新部门
	 * @date 2016年9月1日
	 * @author lich-thinkpad
	 * @param department
	 * @return
	 */
	@RequestMapping(value = "/department/update", method = RequestMethod.POST)
	public ModelAndView updateDepartment(Department department) {
		Result<Department> result = new Result<>();
		Department oldDepartment = departmentService.getByName(department.getName(), department.getId());
		if (oldDepartment != null) {
			result.setSuccess(false);
			result.setMessage("部门名已存在");
			result.setObject(oldDepartment);
			logger.warn("部门名已存在，入参" + JsonUtils.object2Json(department));
			return new ModelAndView(new MappingJackson2JsonView(), "result", result);
		}
		if (departmentService.updateById(department) > 0) {
			result.setSuccess(true);
			result.setMessage("更新部门信息成功");
		} else {
			result.setSuccess(false);
			result.setMessage("更新部门信息失败");
			logger.error("更新部门信息失败，入参" + department.toString());
		}
		return new ModelAndView(new MappingJackson2JsonView(), "result", result);
	}

	/**
	 * @description 删除部门失败
	 * @date 2016年9月2日
	 * @author lich-thinkpad
	 * @param department
	 * @return
	 */
	@RequestMapping(value = "/department/delete", method = RequestMethod.POST)
	public ModelAndView deleteDepartment(int id) {
		Result<List<Department>> result = new Result<>();
		if (departmentService.deleteById(id) > 0) {
			result.setSuccess(true);
			result.setMessage("删除部门信息成功");
		} else {
			result.setSuccess(false);
			result.setMessage("删除部门信息失败");
			logger.error("删除部门失败，入参id=" + id);
		}
		return new ModelAndView(new MappingJackson2JsonView(), "result", result);
	}

	/**
	 * @description 添加页面
	 * @date 2016年8月31日
	 * @author lich-thinkpad
	 * @param product
	 * @return
	 */
	@RequestMapping(value = "/add", method = RequestMethod.GET)
	public ModelAndView addPage() {
		Result<List<Department>> result = new Result<>();
		List<Department> productCategories = departmentService.getAll();
		result.setObject(productCategories);
		result.setSuccess(true);
		result.setMessage("查询产品类型成功");
		return new ModelAndView("manager/product/add", "result", result);
	}

	/**
	 * @description 添加
	 * @date 2016年8月31日
	 * @author lich-thinkpad
	 * @param product
	 * @return
	 */
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public ModelAndView add(Product product) {
		product.setDate(new Date());
		product.setUuid(UUID.randomUUID().toString());
		Result<Product> result = new Result<>();
		Product oldProduct = productService.getByName(product.getName());
		if (oldProduct != null) {
			result.setObject(oldProduct);
			result.setSuccess(false);
			result.setMessage("产品名已存在");
			logger.warn("产品名已存在，入参" + JsonUtils.object2Json(product));
			return new ModelAndView(new MappingJackson2JsonView(), "result", result);
		}
		if (productService.add(product) > 0) {
			result.setSuccess(true);
			result.setMessage("添加产品数据成功");
		} else {
			result.setSuccess(false);
			result.setMessage("添加产品数据失败");
			logger.error("添加产品数据失败，入参" + JsonUtils.object2Json(product));
		}
		return new ModelAndView(new MappingJackson2JsonView(), "result", result);
	}

	/**
	 * @description 更新
	 * @date 2016年8月31日
	 * @author lich-thinkpad
	 * @param product
	 * @return
	 */
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public ModelAndView update(Product product) {
		Result<Product> result = new Result<>();
		Product oldProduct = productService.getByName(product.getName(), product.getId());
		if (oldProduct != null) {
			result.setObject(oldProduct);
			result.setSuccess(false);
			result.setMessage("产品名已存在");
			logger.warn("产品名已存在，入参" + JsonUtils.object2Json(product));
			return new ModelAndView(new MappingJackson2JsonView(), "result", result);
		}
		product.setDate(new Date());
		if (productService.updateById(product) > 0) {
			result.setSuccess(true);
			result.setMessage("更新产品数据成功");
		} else {
			result.setSuccess(false);
			result.setMessage("更新产品数据失败");
			logger.error("更新产品数据失败，入参" + JsonUtils.object2Json(product));
		}
		return new ModelAndView(new MappingJackson2JsonView(), "result", result);
	}

	/**
	 * @description 删除
	 * @date 2016年9月6日
	 * @author lich-thinkpad
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	public ModelAndView delete(int id) {
		Result<Product> result = new Result<>();
		if (productService.deleteById(id) > 0) {
			result.setSuccess(true);
			result.setMessage("删除产品数据成功");
		} else {
			result.setSuccess(false);
			result.setMessage("删除产品数据失败");
			logger.error("删除产品数据失败，入参id=" + id);
		}
		return new ModelAndView(new MappingJackson2JsonView(), "result", result);
	}

	/**
	 * @description 产品信息列表页面
	 * @date 2016年8月31日
	 * @author lich-thinkpad
	 * @param Page<Object>
	 * @return
	 */
	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public ModelAndView getPage(Integer pageNum) {
		Result<PageInfo<ProductResult>> result = new Result<>();
		if(pageNum == null || pageNum <1){
			pageNum = 1;
		}
		PageInfo<ProductResult> productPage = productService.getPage(pageNum);
		result.setObject(productPage);
		result.setSuccess(true);
		result.setMessage("获取产品数据成功");
		return new ModelAndView("manager/product/list", "result", result);
	}

	/**
	 * @description 所有产品
	 * @date 2016年9月12日
	 * @author lich
	 * @return
	 */
	@RequestMapping(value = "/all", method = RequestMethod.GET)
	public ModelAndView getAll() {
		Result<List<Product>> result = new Result<>();
		List<Product> products = productService.getAll();
		result.setObject(products);
		result.setSuccess(true);
		result.setMessage("获取产品数据成功");
		return new ModelAndView(new MappingJackson2JsonView(), "result", result);
	}
}
